<?php
/**
 * func.nct.core.php
 *
 * PHP Version 5
 * 
 * Global functions that can be used all around the framework
 * 
 * @category  Core
 * @package   Core
 * @author    Ervin Musngi <ervinmusngi@gmail.com>
 * 
 * @DateCreated - December 2, 2011
 * @DateModified - December 5, 2011
 * 
 * @copyright 2011 Ervin Musngi
 * @license   http://netcoretech.com Net-Coret-Tech
 * @version   SVN: <tortoise>
 * @link      http://netcoretech.com/php/
 * 
 */

/**
 *@method __verfiy_method_type()
 *
 * @param string $class  - Name of the Class
 * @param string $method - Name of the Class Method
 * 
 * @return bool
 */
function __verify_method_type( $class = '' , $method = '')
{
    $relf = new ReflectionMethod($class, $method);
    
    if ( $relf->isPublic() ) {
        return true;
    } elseif ($relf->isPrivate()) {
        return false;
    } elseif ($relf->isStatic()) {
        return false;
    }
    
}

if ( !function_exists('load_class') ) {
    
    /**
     *@method load_class() Class loader
     * 
     * @param String $class     - Name of the class that will be loaded
     * @param String $directory - The current directory of the class
     * @param String $param     - The parameters that will be put into the constructor
     * 
     * @global String $system_folder
     * @global String $application_folder
     * @staticvar array $_className
     * 
     * @return array 
     */
    function load_class($class,$directory = 'libraries',$param='')
    {
        GLOBAL $system_folder,$application_folder;
        
        static $_className = array();
        
        if ( isset($_className[$class] ) ) {
            return $_className[$class];
        }
        
        if ( file_exists(ROOT_DIR.'/'.$system_folder.'/'.$directory.'/'.$class.'.nct.core.php') ) {
            include_once ROOT_DIR.'/'.$system_folder.'/'.$directory.'/'.$class.'.nct.core.php';
        } else if ( file_exists(ROOT_DIR.'/'.$system_folder.'/'.$directory.'/'.$class.'.nct.library.php') ) {
            include_once ROOT_DIR.'/'.$system_folder.'/'.$directory.'/'.$class.'.nct.library.php';
        } else if ( file_exists(ROOT_DIR.'/'.$system_folder.'/helpers/'.$class.'.nct.helper.php') ) {
            include_once ROOT_DIR.'/'.$system_folder.'/helpers/'.$class.'.nct.helper.php';
        } else if ( file_exists(ROOT_DIR.'/'.$application_folder.'/models/'.$class.'.nct.model.php') ) {
            include_once ROOT_DIR.'/'.$application_folder.'/models/'.$class.'.nct.model.php';
        } else {
            
            if ( ERROR_REPORT === true ) {
                $from = 'load_class()';
                include_once ROOT_DIR.'/'.$system_folder.'/error/unableToloadFile.php';
               
            }
            
            exit();
        }
        
        if ( $param ==''  ) {
            $_className[ $class ] = new $class;
        } else {
            $_className[ $class ] = new $class($param);
        }
        
        return $_className[ $class ];
        
    }
}

if ( !function_exists('get_instance') ) {
    
    /**
     *@method get_instance() Return the current framework instance 
     * 
     * @return Framework Instance
     */
    function get_instance()
    {
        return Controller::get_instance();
    }
    
}

if ( !function_exists('get_config') ) {
    
    /**
     *@method get_config() Get the configuration Array
     * 
     * @param String $name - Name of the configuration file
     * @param String $item - Array Variable of the configuration file
     * 
     * @global String $system_folder
     * @global String $application_folder
     * @return Array 
     */
    function get_config( $name = '' , $item = '')
    {
        GLOBAL $system_folder,$application_folder;
         
        include_once ROOT_DIR.'/'.$application_folder.'/config/'.$name.'.php';
        
        if ( isset(${$item}) ) {
            return ${$item};
        } else {
            return null;
        }
    }
}


if ( !function_exists('autoload_class') ) {
    /**
     *@method autoload_class() Return all classes that will be autoloaded
     * 
     * @return Array
     */
    function autoload_class()
    {
        
        $autoload = get_autoload();
        
        $object = array();
        
        $object = array_merge($autoload['libraries'], $autoload['helpers']);

        return $object;
        
    }
    
}
if ( !function_exists('get_autoload') ) {
    
    /**
     *@method get_autload() Include and return the autload Array Variable from the Autload.php file
     * 
     * @global String $application_folder
     * @return Array 
     */
    function get_autoload()
    {
        GLOBAL $application_folder;
        
        include_once ROOT_DIR.'/'.$application_folder.'/config/autoload.php';
        
        return $autoload;
    }
    
}

if ( !function_exists('autoload_config') ) {
    /**
     *@method autoload_config() Return all config names that will be loaded
     * 
     * @return Array 
     */
    function autoload_config()
    {
        
        $autoload = get_autoload();
        
        return $autoload['config'];
        
    }
    
}

if ( !function_exists('base_url') ) {
    /**
     *@method base_url() Return the current Host URL
     * 
     * @return String The current Host
     * 
     * Here is an inline example :
     * 
     * <code>
     *   echo base_url();
     * </code>
     * 
     * Output :
     * 
     * www.sample.com/
     */
    function base_url()
    {
        
        $config = get_config('config', 'config');
        
        if ( !isset($config['base_url']) || $config['base_url'] == '' ) {
            $base_url = $_SERVER['HTTP_HOST'].'/';
        } else {
            $base_url = $config['base_url'];
        }
        return $base_url;
       
    }
}

if ( !function_exists('site_url') ) {
    /**
     *@method site_url() Return the URI will the base_url
     * 
     * @param String $uri - The URI of the  page
     * 
     * Here is an inline example :
     * 
     * <code>
     *   echo site_url('welcome/home');
     * </code>
     * 
     * Output :
     * 
     * www.sample.com/welcome/home
     * 
     * @return String
     */
    function site_url( $uri = '' )
    {
        $config = get_config('config', 'config');
        
        if ( isset($config['index_page']) && $config['index_page'] != '' ) {
            return base_url().$config['index_page'].'?page='.$uri;
        } else {
            return base_url().$uri;
        }
    }
}

if ( !function_exists('form_error') ) {
    /**
     *@method form_error() Return the validated result from validation class
     * 
     * @param String $key - The Input or Form element name
     * 
     * @return String 
     */
    function form_error( $key = '' )
    {
        if (class_exists('Form_validation')) {
            $res = Form_validation::$instance;
            
            if ( $res !== null ) {
                $resu = $res->get_result();
        
                if ( isset( $resu[$key] ) ) {
                    return $resu[$key];
                }
            }
        }
        
        return;
    }
}

if ( !function_exists('form_value') ) {
    /**
     *@method form_value() Return the post or get value of the element
     * 
     * @param type $key - The Input or Form element name
     * 
     * @return String 
     */
    function form_value( $key = '' )
    {
        if ( class_exists('Form_validation')) {
            $res = Form_validation::$instance;
            if ( $res !== null ) {
                $resu = $res->get_values();
            
                if (isset( $resu[$key] )) {
                    return $resu[$key];
                }
              
            }
        }
        
        return;
    }
}